package org.gaaidou.ptarmigan.springboot.core.common;


import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class FormatUtil {
    private FormatUtil() {
        // nothing to do
    }

    private static final Pattern LINE_PATTERN = Pattern.compile("_(\\w)");
    private static final Pattern HYPHEN_PATTERN = Pattern.compile("-(\\w)");
    private static final Pattern HUMP_PATTERN = Pattern.compile("[A-Z]");


    public static boolean isNumeric(String str) {
        return str.matches("-?[0-9]+.?[0-9]*");
    }


    public static String lineToHump(String str) {
        Matcher matcher = LINE_PATTERN.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String hyphenToHump(String str) {
        Matcher matcher = HYPHEN_PATTERN.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }

    public static String humpToLine(String str) {
        Matcher matcher = HUMP_PATTERN.matcher(str);
        StringBuilder sb = new StringBuilder();
        while (matcher.find()) {
            matcher.appendReplacement(sb, "_" + matcher.group(0).toLowerCase());
        }
        matcher.appendTail(sb);
        return sb.toString();
    }
}
